package com.vancouver.knows.ui.fragment.citynews;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.pangu.service.ActionException;
import com.pangu.task.IResultReceiver;
import com.pangu.task.mark.ATaskMark;
import com.pangu.util.DeviceUtil;
import com.pangu.util.StringUtil;
import com.squareup.picasso.Picasso;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.activity.citydiscount.CityDiscountDetailActivity;
import com.vancouver.knows.ui.activity.citynews.CityNewsDetailActivity;
import com.vancouver.knows.util.AppUtils;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.CommentInfo;
import com.vancouver.knows.business.bean.UserInfo;
import com.vancouver.knows.business.task.mark.UserDeleteUpvoteTaskMark;
import com.vancouver.knows.business.task.mark.UserUpvoteTaskMark;
import com.vancouver.knows.util.CircleTransform;

public class CommentItemView implements IResultReceiver {

	private Activity activity;
	private BusinessModule bModule;
	private List<ViewHolder> viewHolderList = new ArrayList<ViewHolder>();
	private List<CommentInfo> commentInfoList;
	private String postId;
	private LinearLayout commentLayout;

	public void init(Activity activity, LinearLayout rootView, List<CommentInfo> commentInfoList, String postId) {
		this.activity = activity;
		this.postId = postId;
		bModule = ((MainApplication) activity.getApplication()).getBusinessModule();

		if (commentInfoList == null || commentInfoList.size() == 0) {
			return;
		}

		this.commentInfoList = commentInfoList;

		if (rootView != null) {
			int count = rootView.getChildCount();
			rootView.removeViews(1, count - 1);
		}

		commentLayout = new LinearLayout(activity);
		commentLayout.setOrientation(LinearLayout.VERTICAL);
		commentLayout.setDividerDrawable(activity.getResources().getDrawable(R.drawable.linearlayout_line_dirver));
		commentLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
		for (CommentInfo commentInfo : commentInfoList) {
			View view = LayoutInflater.from(activity).inflate(R.layout.comment_item, null);
			ViewHolder viewHolder = new ViewHolder(view);
			viewHolderList.add(viewHolder);
			commentLayout.addView(view);
		}

		if (rootView != null) {
			rootView.addView(commentLayout);
		}

		updateCommentLayout();
	}

	public LinearLayout getCommentLayout() {
		return commentLayout;
	}

	public void updateCommentLayout() {
		for (int i = 0; i < viewHolderList.size(); i++) {
			ViewHolder viewHolder = viewHolderList.get(i);
			CommentInfo commentInfo = commentInfoList.get(i);
			Picasso.with(activity).load(commentInfo.getAvatar()).transform(new CircleTransform()).placeholder(R.drawable.no_avatar).centerCrop()
					.resizeDimen(R.dimen.comment_head_size, R.dimen.comment_head_size)
					.into(viewHolder.commentHead);
			viewHolder.commentAuthor.setText(commentInfo.getAuthor());
			if (!StringUtil.isEmptyString(commentInfo.getParentAuthor())) {
				viewHolder.commentContent.setText(AppUtils.replaceColor(commentInfo.getContent(),
						"@" + commentInfo.getParentAuthor(), activity.getResources().getColor(R.color.orange)));
			} else {
				viewHolder.commentContent.setText(commentInfo.getContent());
			}
			viewHolder.commentTime.setText(AppUtils.getDateStr(activity, commentInfo.getDateGmt()));
			viewHolder.commentUpvote.setText(String.valueOf(commentInfo.getUpvoteCount()));
			if ("Y".equals(commentInfo.getUpvoted())) {
				Drawable drawable = activity.getResources().getDrawable(R.drawable.icon_like_active);
				drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
				viewHolder.commentUpvote.setCompoundDrawables(drawable, null, null, null);
				viewHolder.commentUpvote.setTextColor(activity.getResources().getColor(R.color.orange));
			} else {
				Drawable drawable = activity.getResources().getDrawable(R.drawable.icon_like);
				drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
				viewHolder.commentUpvote.setCompoundDrawables(drawable, null, null, null);
				viewHolder.commentUpvote.setTextColor(activity.getResources().getColor(R.color.common_font));
			}

			viewHolder.commentUpvote.setTag(commentInfo);
			viewHolder.commentAnswer.setTag(commentInfo);
		}
	}

	class ViewHolder {
		@Bind(R.id.commentTime)
		TextView commentTime;
		@Bind(R.id.commentAuthor)
		TextView commentAuthor;
		@Bind(R.id.commentContent)
		TextView commentContent;
		@Bind(R.id.commentAnswer)
		TextView commentAnswer;
		@Bind(R.id.commentUpvote)
		TextView commentUpvote;
		@Bind(R.id.commentHead)
		ImageView commentHead;

		public ViewHolder(View view) {
			ButterKnife.bind(this, view);
		}

		@OnClick(R.id.commentAnswer)
		public void onCommentAnswerClick(View view) {
			if (activity instanceof CityDiscountDetailActivity) {
				((CityDiscountDetailActivity) activity).userInsertParentComment((CommentInfo) view.getTag());

			} else if (activity instanceof CityNewsDetailActivity) {
				((CityNewsDetailActivity) activity).userInsertParentComment((CommentInfo) view.getTag());
			}
		}

		@OnClick(R.id.commentUpvote)
		public void onCommentUpvoteClick(View view) {
//			if (!AppUtils.getLoginStatus(activity)) {
//				return;
//			}
//			UserInfo userInfo = bModule.getCacheManager().getUserInfo();
			UserInfo userInfo = bModule.getCacheManager().getUserInfo();
			String userId = null;
			String auid = null;
			if (userInfo != null) {
				userId = userInfo.getUserID();
			} else {
				auid = ((MainApplication) activity.getApplication()).getSharedPrefManager().getAuid();
			}
			CommentInfo commentInfo = (CommentInfo) view.getTag();
			if ("Y".equals(commentInfo.getUpvoted())) {
				UserDeleteUpvoteTaskMark taskMark = bModule.getTaskMarkPool().getUserDeleteUpvoteTaskMark(
						commentInfo.getId(), "comment", postId);
				bModule.getServiceWrapper().userDeleteUpvote(CommentItemView.this, taskMark, userId, auid,
						commentInfo.getId(), "comment");

			} else {
				UserUpvoteTaskMark taskMark = bModule.getTaskMarkPool().getUserUpvoteTaskMark(commentInfo.getId(),
						"comment", postId);
				bModule.getServiceWrapper().userUpvote(CommentItemView.this, taskMark, userId, auid,
						commentInfo.getId(), "comment");
			}
		}
	}

	@Override
	public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
		if ((taskMark instanceof UserUpvoteTaskMark || taskMark instanceof UserDeleteUpvoteTaskMark)
				&& taskMark.getTaskStatus() == ATaskMark.HANDLE_OVER) {
			updateCommentLayout();
		}
	}

}
